<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>

var arr = [1,2,3,4,5,6,7,8,9,10]
        function searching(tar) {
            var start = 0,
                end = arr.length - 1,
                middle,
                element
            // while 循环会在指定条件为真时循环执行代码块。
            while (start <= end) {
                middle = Math.floor((start + end) / 2);
                console.log('中位数:',middle);
                element = arr[middle]
                console.log('中位数对应的值:',element);
                if(element === tar){
                    return middle;
                } else if(tar < element){
                    end = middle - 1
                } else {
                    start = middle + 1
                }
            }
        }
        console.log(searching(9))


      function strFastSearch(arr, key) {
      var low = 0;
      var high = arr.length - 1;

      while (low <= high) {
        var mid = parseInt((high + low) / 2);

        if (key === arr[mid]) {
          return mid;
        } else if (key > arr[mid]) {
          low = mid + 1;
        } else if (key < arr[mid]) {
          high = mid - 1;
        } else {
          return -1;
        }
      }
    }
    var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    var result = strFastSearch(arr, 3);
    console.log(result)
</script>
</html>